package com.prj.util;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;


public class PdfToText {
	
	public static String deal(String pdfPath){
		String converText = converText(pdfPath);
		String removeBlank = removeBlank(converText);
		return removeBlank;
	}
	
	
	public static String converText(String pdfPath){
		try {
			File pdf = new File(pdfPath);
			PDDocument document = PDDocument.load(new File(pdfPath));
			PDFTextStripper pdfStripper = new PDFTextStripper();
			String text = pdfStripper.getText(document);
			File file = new File(pdf.getParent(), pdf.getName().replace(".pdf", ".txt"));
			FileUtils.writeStringToFile(file, text, "UTF-8");
			System.out.println("pdf转换txt完成");
			return file.getAbsolutePath();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("pdf转换txt异常");
		}
		return "";
	}
	
	public static String removeBlank(String path){
		try {
			File txt = new File(path);
			List<String> readLines = FileUtils.readLines(txt, "UTF-8");
			String reg = "^[0-9A-F].*";
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i <= readLines.size()-2; i++){
				sb.append(readLines.get(i));
				String str = readLines.get(i+1).trim();
				if(str.matches(reg) || str.startsWith("健康管理师（三级）") || str.startsWith("答案") || str.startsWith("解析")){
					sb.append("\n");
				}
				
				if(str.matches("^[0-9].*") ){
					sb.append("\n");
				}
			}
			File file = new File(txt.getParent(), txt.getName().replace(".txt","2.txt"));
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			System.out.println("格式化完成");
			if(txt.exists()){
				txt.delete();
			}
			return file.getAbsolutePath();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("格式化异常");
		}
		return "";
	}
	
	/**
	 * 手工调整并将第二行的空行去掉、以及文件最未尾增加空行
	 * @param txtPath
	 * @param adjFile 是否生成调整文件
	 * @return
	 */
	public static String adjAfter(String txtPath, boolean adjFile){
		try {
			
			File txt = new File(txtPath);
			List<String> readLines = FileUtils.readLines(txt, StandardCharsets.UTF_8);
			StringBuilder sb = new StringBuilder();
			for(int i = 0; i < readLines.size(); i++){
				String line = readLines.get(i).trim();
				if(!line.startsWith("健康管理师（三级）")){
					try {
						String nextline = readLines.get(i+1).trim();
						if(  (line.matches("^[0-9].*") && !nextline.startsWith("A")) || 
							 (line.startsWith("A") && !nextline.startsWith("B")) || 
							 (line.startsWith("B") && !nextline.startsWith("C")) ||
							 (line.startsWith("C") && !nextline.startsWith("D")) ||
							 (line.startsWith("D") && !(nextline.startsWith("E") || nextline.startsWith("答案") )) ||
							 (line.startsWith("E") && !(nextline.startsWith("F") || nextline.startsWith("答案") )) ||
							 (line.startsWith("F") && !nextline.startsWith("答案")) || 
							 (line.startsWith("答案") && !(nextline.startsWith("解析") ||  nextline.isEmpty() || nextline.startsWith("健康管理师（三级）") )) ||    
							 (line.startsWith("解析") && !(nextline.isEmpty() || nextline.startsWith("健康管理师（三级）") ))   
								){
							sb.append(i+1).append("行，行内容：").append(line).append("\n").append("下一行内容：").append(nextline).append("\n\n");
						}
						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
			if(adjFile) {
				File adj = new File(txt.getParent(), "adjust.txt");
				FileUtils.write(adj, sb.toString(), StandardCharsets.UTF_8);
			}
			System.out.println("finished:"+sb.toString());
			return sb.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void main(String[] args) {
//		String deal = deal("C:\\Users\\Administrator\\Desktop\\广东省健康管理师考前复习.pdf");
//		System.out.println(deal);
		String txtPath = "C:\\Users\\Administrator\\Desktop\\广东省健康管理师考前复习2.txt";
		adjAfter(txtPath,false);
	}
	
	
	
	
	
	
	
	

}
